<template>
  <a-spin :spinning="spinning != 0">
      <a-tabs v-model="activeKey" @change="changeTab" v-if="iProps.erModel" style="top:0px;margin-bottom: 0px;">
        <a-tab-pane :tab="iProps.erModel.relation.tableComment" :key="iProps.erModel.relation.tableKey" :forceRender="true">
          <table-fields :ref="iProps.erModel.relation.tableKey" :genConfigId="genConfigId" :tableName="iProps.erModel.relation.tableKey" ></table-fields>
        </a-tab-pane>
        <a-tab-pane v-if="iProps.erModel.relation.children" v-for="child in iProps.erModel.relation.children" :tab="child.tableComment" :key="child.tableKey" :forceRender="true">
          <table-fields :ref="child.tableKey" :genConfigId="genConfigId" :tableName="child.tableKey" ></table-fields>
        </a-tab-pane>
      </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction, httpAction } from '@/api/manage'
  import TableFields from './TableFields'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'FormAndViewConfig',
    mixins: [],
    components: {
      TableFields
    },
    props: {
      iProps: {
        type: Object,
        default: () => {
        }
      },
      genConfigId:{
        type:String,
        default:""
      }

    },
    data () {
      return {
        spinning: 0,//遮罩
        activeKey:this.iProps.erModel.relation.tableKey,
        url:{
          addTableColumns:YchConstant.PREFIX_EFORM + "/generator/manage/genTableColumn/addTableColumns"
        }
      }
    },
    methods: {
      show(){

      },
      save(){
        let that = this
        let subTableArr = []
        let mainTable = {}
        let mainTableRef = this.$refs[this.iProps.erModel.relation.tableKey]
        let mainTableData = mainTableRef.getData()
        mainTable.genConfigId = this.genConfigId
        mainTable.tableName = this.iProps.erModel.relation.tableKey
        mainTable.cloumnValues = mainTableData
        mainTable.controlCount = mainTableRef.controlCount
        subTableArr.push(mainTable)
        if(this.iProps.erModel.relation.children && this.iProps.erModel.relation.children.length>0){
          this.iProps.erModel.relation.children.forEach(
            table=>{
              let subTableData = {}
              let controlCount = 2;
              let subTableRef = that.$refs[table.tableKey]
              if(subTableRef instanceof Array){
                subTableData = subTableRef[0].getData()
                controlCount = subTableRef[0].controlCount
              }else{
                subTableData = subTableRef.getData()
                controlCount = subTableRef.controlCount
              }

              let subTable = {}
              subTable.genConfigId = that.genConfigId
              subTable.tableName = table.tableKey
              subTable.cloumnValues = subTableData
              subTable.controlCount = controlCount
              subTableArr.push(subTable)
            }
          );
        }
        let method = 'post'
        that.spinning++
        let promise = new Promise(function(resolve, reject) {
          httpAction(that.url.addTableColumns, subTableArr, method).then((res) => {
            if (res.success) {
              that.messageUtil.success(res.message)
              resolve(res.message)
            } else {
              that.messageUtil.warning(res.message)
              reject(res.message)
            }
          }).finally(() => {
            that.spinning--
          })
        });
        return promise;
      },
      changeTab (activeKey) {
        this.activeKey = activeKey
      }
    },
    watch: {

    }
  }
</script>
